package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;

    // 查看商品详情业务
    @Override
    public ProductDetailDto getProductDetail(Long id) {
        Item item = itemMapper.selectByPrimaryKey(id);
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);
        return productDetailDto;
    }

    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        // 启用分页
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getSize());
        Example example = new Example(Item.class);
        String sort = pageInfo.getSort();
        // 请求未指定排序规则，默认根据创建时间created排序
        if (sort == null || "".equals(sort)) {
            example.setOrderByClause("created asc");
        } else {
            // 指定排序规则，1表示升序，-1表示降序
            int i = Integer.parseInt(sort);
            if (i == 1) {
                example.setOrderByClause("price asc");
            } else {
                example.setOrderByClause("price desc");
            }
        }
        // 指定价格区间
        Integer priceGt = pageInfo.getPriceGt();
        Integer priceLte = pageInfo.getPriceLte();
        Example.Criteria criteria = example.createCriteria();
        if (priceGt != null){
            criteria.andGreaterThanOrEqualTo("price", priceGt);
        }
        if (priceLte != null){
            criteria.andLessThanOrEqualTo("price", priceLte);
        }
        // 指定cid
        Long cid = pageInfo.getCid();
        if (cid != null){
            criteria.andEqualTo("cid", cid);
        }
        // 查询所有商品
        List<Item> items = itemMapper.selectByExample(example);
        // 封装对象返回
        // todo:此处需把查询结果原始的list放入到PageInfo的有参构造方法中，若将遍历处理后的list放入，结果会有偏差
        PageInfo<Item> productDtoPageInfo = new PageInfo<>(items);
        AllProductResponse allProductResponse = new AllProductResponse();
        allProductResponse.setTotal(productDtoPageInfo.getTotal());
        // 分页处理后再做Do→Dto转换
        List<ProductDto> productDtos = productConverter.items2Dto(productDtoPageInfo.getList());
        allProductResponse.setData(productDtos);
        return allProductResponse;
    }

    @Override
    public Set<PanelDto> getRecommendGoods() {
        Set<PanelDto> panelDtoSet = panelMapper.selectPanelContentByPosition(1);
        return panelDtoSet;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }
}
